* ==============================================================================
*	Title:	'They Take Our Houses': Benefit Competition and the Erosion of Support for Immigrants’ Social Rights
*	Author:	Gerda Hooijer
*	Date:	February 28, 2020
* 	Replication do file for: Main dataset
* ==============================================================================
/* 
*** This file:
(1) combines waves 1-9 of the LISS Politics and Values modules and Background Variables
(2) adds a small number of variables from other LISS modules
(3) adds the regional identifier
(4) combines datasets from 1), 2), and 3)
(5) recodes the variables
(6) merges the LISS data to the Dutch Municipalities Dataset

*** Required LISS original source files:

** From the Politics and Values module:
cv08a_1.1p_EN.dta
cv09b_2.1p_EN.dta
cv10c_EN_1.0p.dta
cv11d_EN_1.0p.dta
cv12e_EN_1.0p.dta
cv13f_EN_1.0p.dta
cv14g_EN_1.0p.dta
cv16h_EN_1.0p.dta
cv17i_EN_1.0p.dta

** From the Background Variables module:
avars_200712_EN_2.0p.dta
avars_200803_EN_2.0p.dta
avars_200812_EN_2.0p.dta
avars_200901_EN_2.0p.dta
avars_200912_EN_2.0p.dta
avars_201001_EN_2.0p.dta
avars_201002_EN_2.0p.dta
avars_201101_EN_2.0p.dta
avars_201012_EN_2.0p.dta
avars_201112_EN_2.0p.dta
avars_201201_EN_2.0p.dta
avars_201212_EN_1.0p.dta
avars_201301_EN_1.0p.dta
avars_201312_EN_1.0p.dta
avars_201401_EN_1.0p.dta
avars_201512_EN_1.0p.dta
avars_201601_EN_1.0p.dta
avars_201612_EN_1.0p.dta
avars_201701_EN_1.0p.dta

** Self-assembled through the LISS Data Archive (see more under section 2 below):
class_temp.dta
cob_temp.dta 

*** Files from other sources:
NL_cpi.dta 

*/

version 16

set more off 

// set directory


* ============================
* (1) COMBINING WAVES 1 TO 9 
* ============================

/* Note: Political attitudes are part of the Politics and Values module. The main fieldwork period is December but some respondents receive a reminder and complete the survey in January. The module can be downloaded for each wave separately. (Alternatively, you can add the required variables to your basket and download these in a custom-made dataset.) Basic sociodemographic variables are stored in the Background Variables module and will be matched for each wave of the Politics and Values module. 
*/

** Creating the 'wave' variables, only needed during the first run:


* Wave 1
use "cv08a_1.1p_EN.dta", clear 
clonevar wave = cv08a_m
gen wave_num = 1 
save "cv08a_1.1p_EN.dta", replace
*
* Wave 2
use "cv09b_2.1p_EN.dta", clear
clonevar wave = cv09b_m
gen wave_num = 2
save "cv09b_2.1p_EN.dta", replace
*
* Wave 3
use "cv10c_EN_1.0p.dta", clear
clonevar wave = cv10c_m
gen wave_num = 3
save "cv10c_EN_1.0p.dta", replace
*
* Wave 4 
use "cv11d_EN_1.0p.dta", clear
clonevar wave = cv11d_m
gen wave_num = 4
save "cv11d_EN_1.0p.dta", replace
*
* Wave 5 
use "cv12e_EN_1.0p.dta", clear
clonevar wave = cv12e_m
gen wave_num = 5
save "cv12e_EN_1.0p.dta", replace 
*
* Wave 6
use "cv13f_EN_1.0p.dta", clear
clonevar wave = cv13f_m
gen wave_num = 6
save "cv13f_EN_1.0p.dta", replace 
*
* Wave 7
use "cv14g_EN_1.0p.dta", clear
clonevar wave = cv14g_m
gen wave_num = 7
save "cv14g_EN_1.0p.dta", replace
*
* Wave 8
use "cv16h_EN_1.0p.dta", clear
clonevar wave = maandnr 
replace wave = maandnr_lang if wave==. 
replace wave = maandnr_deel1 if wave==.
replace wave = maandnr_a if wave==.
tab wave, missing
gen wave_num = 8
save "cv16h_EN_1.0p.dta", replace 
*
* Wave 9
use "cv17i_EN_1.0p.dta", clear
clonevar wave = cv17i_m
gen wave_num = 9
save "cv17i_EN_1.0p.dta", replace


** Merging the Background Variables and Politics and Values modules 

* Wave 1
use "avars_200712_EN_2.0p.dta", clear 
append using "avars_200803_EN_2.0p.dta"
merge 1:1 nomem_encr wave using "cv08a_1.1p_EN.dta"
keep if _merge==2 | _merge==3
drop _merge 
rename *cv08a* *var* 
save "LISS_1.dta", replace

* Wave 2
use "avars_200812_EN_2.0p.dta", clear 
append using "avars_200901_EN_2.0p.dta" 
merge 1:1 nomem_encr wave using "cv09b_2.1p_EN.dta"
keep if _merge==2 | _merge==3
drop _merge 
rename *cv09b* *var*
save "LISS_2.dta", replace

* Wave 3
use "avars_200912_EN_2.0p.dta", clear
append using "avars_201001_EN_2.0p.dta"
append using "avars_201002_EN_2.0p.dta"
merge 1:1 nomem_encr wave using "cv10c_EN_1.0p.dta"
keep if _merge==2 | _merge==3
drop _merge 
rename *cv10c* *var* 
save "LISS_3.dta", replace

* Wave 4 
use "avars_201101_EN_2.0p.dta", clear
append using "avars_201012_EN_2.0p.dta"
merge 1:1 nomem_encr wave using "cv11d_EN_1.0p.dta"
keep if _merge==2 | _merge==3
drop _merge 
rename *cv11d* *var*
save "LISS_4.dta", replace

* Wave 5 
use "avars_201112_EN_2.0p.dta", clear
append using "avars_201201_EN_2.0p.dta"
merge 1:1 nomem_encr wave using "cv12e_EN_1.0p.dta"
keep if _merge==2 | _merge==3
drop _merge 
rename *cv12e* *var*
save "LISS_5.dta", replace

* Wave 6
use "avars_201212_EN_1.0p.dta", clear
append using "avars_201301_EN_1.0p.dta"
merge 1:1 nomem_encr wave using "cv13f_EN_1.0p.dta" 
keep if _merge==2 | _merge==3
drop _merge
rename *cv13f* *var*
save "LISS_6.dta", replace

* Wave 7
use "avars_201312_EN_1.0p.dta", clear
append using "avars_201401_EN_1.0p.dta"
merge 1:1 nomem_encr wave using "cv14g_EN_1.0p.dta"
keep if _merge==2 | _merge==3
drop _merge
rename *cv14g* *var*
save "LISS_7.dta", replace

* Wave 8
use "avars_201512_EN_1.0p.dta", clear
append using "avars_201601_EN_1.0p.dta"
merge 1:1 nomem_encr wave using "cv16h_EN_1.0p.dta" 
keep if _merge==3
drop _merge 
rename *cv16h* *var*
save "LISS_8.dta", replace

* Wave 9
use "avars_201612_EN_1.0p.dta", clear
append using "avars_201701_EN_1.0p.dta" 
merge 1:1 nomem_encr wave using "cv17i_EN_1.0p.dta"
keep if _merge==3 | _merge==2
drop _merge 
rename *cv17i* *var*
save "LISS_9.dta", replace


** Append all waves

use "LISS_1", clear
append using "LISS_2" 
append using "LISS_3"
append using "LISS_4"
drop var162 var164
append using "LISS_5"
append using "LISS_6"
append using "LISS_7"
append using "LISS_8"
drop var162 var164
append using "LISS_9"

* Generate year variable
gen year = .
replace year = 2007 if wave==200712 | wave==200803
replace year = 2008 if wave==200812 | wave==200901
replace year = 2009 if wave==200912 | wave==201001 | wave==201002
replace year = 2010 if wave==201012 | wave==201101
replace year = 2011 if wave==201112 | wave==201201
replace year = 2012 if wave==201212 | wave==201301
replace year = 2013 if wave==201312 | wave==201401
replace year = 2015 if wave==201512 | wave==201601
replace year = 2016 if wave==201612 | wave==201701
tab year, missing
label var year "Year"

save "liss_1to9", replace  

** remove excess files
erase "LISS_1.dta"
erase "LISS_2.dta"
erase "LISS_3.dta"
erase "LISS_4.dta"
erase "LISS_5.dta"
erase "LISS_6.dta"
erase "LISS_7.dta"
erase "LISS_8.dta"
erase "LISS_9.dta"

* =========================================
* (2) CLEAN VARIABLES FROM OTHER MODULES
* =========================================
/*
The analysis requires two variables that were collected in other modules of the LISS. In the LISS Data Archive, these variables can be added to a basket and downloaded.

* Social class: variable cw***404 from the Work and Schooling module, which is conducted in April/May of each year. The variable can be found by using the search term "current profession".
Note: save the file as *class_temp.dta*

* Country of birth: For 2007-2009, this variable was included in the Religion and Ethnicity module (cr***053) while for 2010-2016 it was measured only once for new panel members in the Initial Questionnaire (ea***124). The variables can be found by using the search terms "Were you born in the Netherlands".
Note: save the file as *cob_temp.dta*
*/

** Social class 
use "class_temp", clear
rename cw08a404 class2008
rename cw09b404 class2009
rename cw10c404 class2010
rename cw11d404 class2011
rename cw12e404 class2012
rename cw13f404 class2013
rename cw14g404 class2014
rename cw15h404 class2015
rename cw16i404 class2016
reshape long class, i(nomem_encr) j(year)
label var class "Social class"
save "liss_class.dta", replace 

** Country of birth
use "cob_temp", clear
rename cr08a053 cob2007
rename cr09b053 cob2008
rename cr10c053 cob2009
rename ea10a124 cob2010
rename ea11a124 cob2011
rename ea12a124 cob2012
rename ea13a124 cob2013
rename ea14a124 cob2014
rename ea15a124 cob2015
reshape long cob, i(nomem_encr) j(year)
recode cob (1=1) (2=0) (99=.), into(native)
label var native "Native-born"
// keep one observation per respondent
drop year 
drop if native==. 
bysort nomem_encr: gen dup = cond(_N==1,0,_n)
drop if dup>1
drop dup 
save "liss_cob.dta", replace 


* =====================================
* (3) REGIONAL IDENTIFIER DATA
* =====================================

/* 
The regional identifier (respondent's municipality of residence in January of each year) was provided by LISS as an SPSS file in wide format. This can be imported directly in Stata 16 or with the user-written Stata ado-file usespss (for earlier versions). Each municipality variable corresponds to each of the nine Politics and Values modules conducted in December/January between 2007 and 2016.
*/

import spss using "municipality_2008-2016_V3.sav", clear
rename municipality2008_pov_reclass municipality1
rename municipality2009_pov_reclass municipality2
rename municipality2010_pov_reclass municipality3
rename municipality2011_pov_reclass municipality4
rename municipality2012_pov_reclass municipality5
rename municipality2013_pov_reclass municipality6
rename municipality2014_pov_reclass municipality7
rename municipality2015_pov_reclass municipality8
rename municipality2016_pov_reclass municipality9
reshape long municipality, i(nomem_encr) j(wave_num)
label var wave_num "Wave number"
label var municipality "Municipality, string"
save "liss_mun", replace 


* ======================================
* (4) COMBINE ALL DATASETS FROM ABOVE 
* ======================================

use "liss_1to9", replace
merge 1:1 nomem_encr wave_num using "liss_mun"
drop if _merge==2
drop _merge
merge 1:1 nomem_encr year using "liss_class"
drop if _merge==2
drop _merge 
merge m:1 nomem_encr using "liss_cob" 
drop if _merge==2
drop _merge
merge m:1 year using "NL_cpi"
keep if _merge==3
drop _merge  
// keep main variables for the analysis:
keep nomem_encr year wave wave_num municipality leeftijd geslacht aantalhh belbezig oplcat nettohh_f var119 var101 class var106 brutohh_f woning var134 var120 var123 var116 var085 var175 var213 cpi native sted
// save dataset
save "liss_short", replace  


* =======================
* (5) RECODE VARIABLES
* =======================
// the replication files include a mock version of this dataset
use "liss_short", clear 

* Age
codebook leeftijd, tab(50)
clonevar age = leeftijd 
label var age "Age (in years)"

* Sample of native-born adults 
gen sample=1 if age>17 & age<. & native==1
label var sample "Native-born adults"

* Female
recode geslacht (1=0) (2=1), into(female)
label var female "Female"

* Household size
codebook aantalhh, tab(50) 
clonevar hhsize = aantalhh
label var hhsize "Household size"

* Unemployed 
codebook belbezig, tab(50)
recode belbezig (4/5=1) (1/3 6/14=0), into(unemployed) 
label var unemployed "Unemployed"

* Inactive
recode belbezig (1/5=0) (6/14=1), into(inactive) 
label var inactive "Inactive"

* University degree
codebook oplcat, tab(50) 
recode oplcat (1/5=0) (6=1), into(degree)
label var degree "University degree"

* Net monthly household income
gen income = nettohh_f / 1000
label var income "Net household income (x 1,000)"
// income corrected for inflation
gen income_cpi = income * cpi / 100
label var income_cpi "Net household income (x CPI 1,000)" 
// censor extreme values
clonevar income_cpi3 = income_cpi
sum income_cpi if sample==1, detail
replace income_cpi3 = r(mean) + 3*r(sd) if income_cpi3>(r(mean) + 3*r(sd)) & income_cpi3<.

* Secondary target group
gen sec_target = 0 if income_cpi3<=1.8
replace sec_target = 1 if income_cpi3>1.8 & income_cpi3<. 
label var sec_target "Secondary target group"

* Support for immigrants' social rights
codebook var119, tab(50)
clonevar include1 = var119
label var include1 "Support for equal rights, 1-5"
recode var119 (1/3=0) (4/5=1), into(include2)
label var include2 "Support for equal rights, 0-1"
recode var119 (1/4=0) (5=1), into(strongly_agree)
label var strongly_agree "Strongly support equal rights, 0-1"

* Change in support for immigrants' social rights compared to the previous period
xtset nomem_encr year
gen d_include2 = include2 - L.include2
replace d_include2 = include2 - L2.include2 if d_include2==.
replace d_include2 = include2 - L3.include2 if d_include2==.
replace d_include2 = include2 - L4.include2 if d_include2==.
replace d_include2 = include2 - L5.include2 if d_include2==.
label define d_include2 -1 "Less" 0 "Same" 1 "More"
label values d_include2 d_include2
label var d_include2 "Change in support"

* Left-right ideology
codebook var101, tab(50)
recode var101 (999=.), into(ideology)  
label var ideology "Left-right scale, 10=Right"

* Student
codebook belbezig, tab(50) 
recode belbezig (7=1) (1/6 8/14=0), into(student)
label var student "Student"

* Retired
recode belbezig (9=1) (1/8 10/14=0), into(retired)
label var retired "Retired"

* Social class 
clonevar soc_class = class
replace soc_class = 10 if class==. & student==1 | class==. & retired==1
label define soc_class 1 "Higher academic profession" 2 "Higher supervisory profession" 3 "Intermediate academic profession" 4 "Intermediate supervisory profession" 5 "Other mental work" 6 "Skilled manual work" 7 "Semi-skilled manual work" 8 "Unskilled manual work" 9 "Agrarian profession" 10 "Students and retired" 
label values soc_class soc_class 
label var soc_class "Social class"

* Authoritarianism proxy
codebook var106, tab(50)
recode var106 (1=1) (2/4=0), into(laworder)
label var laworder "Prioritize order"

* Gross monthly household income
gen gross = brutohh_f / 1000
label var gross "Gross household income (x 1,000)"
// income corrected for inflation
gen gross_cpi = gross * cpi / 100
label var gross_cpi "Gross household income (x CPI 1,000)" 
// censor extreme values
clonevar gross_cpi3 = gross_cpi
sum gross_cpi if sample==1, detail
replace gross_cpi3 = r(mean) + 3*r(sd) if gross_cpi3>(r(mean) + 3*r(sd)) & gross_cpi3<.

* Type of dwelling 
codebook woning, tab(50)
recode woning (1=1) (2 4=0) (9=.), into(homeowner) 
label var homeowner "Homeowner"

* Take care of sick parents
codebook var134, tab(50)
recode var134 (99=.) (1/3=0) (4/5=1), into(care_parents)
label var care_parents "Children should take care of sick parents"

* Pro-immigration index
codebook var120, tab(50)
recode var120 (1=5) (2=4) (3=3) (4=2) (5=1), into(var120_rev) // reversed
codebook var123, tab(50)
recode var123 (1=5) (2=4) (3=3) (4=2) (5=1), into(var123_rev) // reversed
gen pro_immi = var116 + var120_rev + var123_rev + var119
label var pro_immi "Pro-immigration index"

* Sympathy for Party for Freedom (Partij voor de Vrijheid)
// Because the variable name changes across waves, create temporary measures first and combine them into a single variable.
codebook var085, tab(50)
recode var085 (999=.), into(temp1)
codebook var175, tab(50)
recode var175 (999=.), into(temp2)
codebook var213, tab(50)
recode var213 (999=.), into(temp3)
gen pvv_symp = .
replace pvv_symp = temp1 if pvv_symp==.
replace pvv_symp = temp2 if pvv_symp==.
replace pvv_symp = temp3 if pvv_symp==.
label var pvv_symp "Sympathy for PVV, 0-10"
drop temp1-temp3
recode pvv_symp (0/6=0) (7/10=1), into(pvv_symp2)
label var pvv_symp2 "Sympathy for PVV, 0-1"

* save dataset 
xtset nomem_encr year

save "liss_short", replace 


* ======================================================
* (6) MERGE LISS DATA WITH DUTCH MUNICIPALITY DATASET
* ======================================================
use "liss_short", clear 
merge m:1 municipality year using "municipality"
drop if _merge==2
drop _merge 
// save dataset
save "liss_L1L2", replace 